package robot.devices;

import java.util.ArrayList;

import robot.Robot;
import robot.exception.RobotException;
import robot.protocol.DATA_DESCRIPTOR;
import robot.protocol.MODULE;
import robot.utils.Checked;
import robot.utils.Convertions;
import robot.utils.NotChecked;

/**
 * This class provides all functions for the battery management. A battery is a 
 * specific device, cause there is more than a simple data descriptor to obtain 
 * interested values.
 * 
 * @see Device
 * @see DATA_DESCRIPTOR
 * 
 * @author Emmanuel CHAFFRAIX
 * @author Francois ROBERT
 *
 */
public final class Battery extends Device {	

	/**
	 * 
	 * Classic constructor which specifies the Module, the data descriptor of the 
	 * battery status and the mode. The battery is read-only. 
	 * 
	 * @param robot The reference robot. Need it for the connection.
	 */
	@Checked
	public Battery(Robot robot) {
		super(robot, MODULE.MAIN, DATA_DESCRIPTOR.BAT_STATUS);
	}
	

	/**
	 * Check if the battery is connected with a power source.
	 * 
	 * @return True if the cable is plugged or false.
	 * @throws RobotException If the robot is disconnected and the reset fails.
	 */
	@NotChecked
	public boolean isInCharge () throws RobotException {
		byte[] receive = this.getParameter(1, DATA_DESCRIPTOR.BAT_VOLTAGE);

		return (receive[0] < 0);
	}


	/**
	 * Gets the status of the battery.
	 * 
	 * @return The percentage of the battery
	 * @throws RobotException If the robot is disconnected and the reset fails.
	 */
	@Checked
	public byte getGauge () throws RobotException {
		byte[] receive = this.getParameter(1, DATA_DESCRIPTOR.BAT_GAUGE);

		return (receive[0]);
	}


	/**
	 * Gets the voltage of the battery.
	 * 
	 * @return The current Voltage in milli-Volt
	 * @throws RobotException If the robot is disconnected and the reset fails.
	 */
	@Checked
	public int getVoltage () throws RobotException {
		byte[] receive = this.getParameter(2, DATA_DESCRIPTOR.BAT_VOLTAGE);

		return Convertions.toInt(receive);
	}
	

	/**
	 * Gets the electric current of the battery.
	 * 
	 * @return The present electric current in milli-Ampere
	 * @throws RobotException If the robot is disconnected and the reset fails.
	 */
	@Checked
	public int getCurrent () throws RobotException {
		byte[] receive = this.getParameter(2, DATA_DESCRIPTOR.BAT_CURRENT);
		
		return Convertions.toInt(receive);
	}
}
